﻿using System;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;

using System.Web.UI;

using System.IO;
using System.Data;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;
using System.Web;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using NPOI.HSSF.UserModel;

using AjaxPro;
using Gift.Model;
using Gift.DAL;

namespace Gift.WebApp
{

    /// <summary>
    ///Region_ajax 的摘要说明
    /// </summary>
    public class Region_ajax
    {
        // Fields

        readonly DALAttributes theAttributes = new DALAttributes();
        readonly DALBrand theBrand = new DALBrand();
        readonly DALGoodsEntitys theGoodsEntitys = new DALGoodsEntitys();
        readonly DALGoodGallery theGoodGallery = new DALGoodGallery();
        readonly DALSpecifications theSpecifications = new DALSpecifications();
        readonly DALSpecificationsValue theSpecificationsValue = new DALSpecificationsValue();
        readonly DALAttributeValue theAttributeValue = new DALAttributeValue();

        // Methods
        [AjaxMethod]
        public bool CheckBarCode(object barCode, object geId)
        {
            try
            {
                return this.theGoodsEntitys.CheckBarCode(barCode.ToString(), Convert.ToInt32(geId));
            }
            catch
            {
                return true;
            }
        }

        [AjaxMethod]
        public bool CheckGoodsArtno(object goodsArtno, object geId)
        {
            try
            {
                return this.theGoodsEntitys.CheckGoodsArtno(goodsArtno.ToString(), Convert.ToInt32(geId));
            }
            catch
            {
                return true;
            }
        }

        [AjaxMethod]
        public bool DeleteGoodGalleryById(object id)
        {
            bool flag = false;
            try
            {
                tbGoodGallery oGoodGallery  = this.theGoodGallery.GetModel(Convert.ToInt32(id));
                if (this.theGoodGallery.Delete(Convert.ToInt32(id)))
                {
                    flag = true;
                    File.Delete(HttpContext.Current.Server.MapPath(oGoodGallery.thumbImg));
                    File.Delete(HttpContext.Current.Server.MapPath(oGoodGallery.smallImg));
                    File.Delete(HttpContext.Current.Server.MapPath(oGoodGallery.bigImg));
                }
            }
            catch
            {
            }
            return flag;
        }

        [AjaxMethod]
        public bool DeleteGoodsEntitysById(object id)
        {
            bool flag = false;
            try
            {
                if (this.theGoodsEntitys.Delete(Convert.ToInt32(id)))
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public Array GetAtrributesByGoodsType(object goodsType, object ruId)
        {
            ArrayList list = new ArrayList();
            try
            {
                DataTable atrributesByGoodsTypeAndState = this.theAttributes.GetAtrributesByGoodsTypeAndState(Convert.ToInt32(goodsType), 1);
            }
            catch
            {
            }
            return list.ToArray();
        }

        [AjaxMethod]
        public string GetAtrributesForm(object goodsType, object state)
        {
            string str = "";
            try
            {
                DataTable atrributesByGoodsTypeAndState = this.theAttributes.GetAtrributesByGoodsTypeAndState(Convert.ToInt32(goodsType), Convert.ToInt32(state));
                if (atrributesByGoodsTypeAndState.Rows.Count <= 0)
                {
                    return str;
                }
                str = str + "<table cellspacing=\"0\" cellpadding=\"0\">";
                foreach (DataRow row in atrributesByGoodsTypeAndState.Rows)
                {
                    DataTable attributeValueByAttribute;
                    DataTable table3;
                    object obj2;
                    string str3;
                    if (Convert.ToInt32(row["isRequired"]) == 0)
                    {
                        switch (Convert.ToInt32(row["attributeInputType"]))
                        {
                            case 0:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" });
                                goto Label_0927;

                            case 1:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" });
                                goto Label_0927;

                            case 2:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<65 || event.keyCode>90)&&(event.keyCode<97 || event.keyCode>122)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" });
                                goto Label_0927;

                            case 3:
                                attributeValueByAttribute =  this.theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\">" });
                                foreach (DataRow row2 in attributeValueByAttribute.Rows)
                                {
                                    str3 = str;
                                    str = str3 + "<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" />" + row2["value"].ToString() + "";
                                }
                                str3 = str;
                                str = str3 + "<input name=\"hdAtrributes\" type=\"hidden\" value=\"rdAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>";
                                goto Label_0927;

                            case 4:
                                table3 =  theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\">" });
                                foreach (DataRow row3 in table3.Rows)
                                {
                                    str3 = str;
                                    str = str3 + "<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" />" + row3["value"].ToString();
                                }
                                str = str + "</td></tr>";
                                goto Label_0927;

                            case 5:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input id=\"txtAtrributes", row["id"].ToString(), "\" name=\"atrributesValue\" type=\"text\" onfocus=\"new WdatePicker(this,'%Y-%M-%D',true,'default')\" style=\"ime-mode:disabled\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" });
                                goto Label_0927;
                        }
                    }
                    else
                    {
                        switch (Convert.ToInt32(row["attributeInputType"]))
                        {
                            case 0:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" });
                                goto Label_0927;

                            case 1:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" });
                                goto Label_0927;

                            case 2:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<65 || event.keyCode>90)&&(event.keyCode<97 || event.keyCode>122)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" });
                                goto Label_0927;

                            case 3:
                                attributeValueByAttribute = theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\">" });
                                foreach (DataRow row2 in attributeValueByAttribute.Rows)
                                {
                                    str3 = str;
                                    str = str3 + "<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" />" + row2["value"].ToString() + "";
                                }
                                str3 = str;
                                str = str3 + "<input name=\"hdAtrributes\" type=\"hidden\" value=\"rdAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>";
                                goto Label_0927;

                            case 4:
                                table3 = theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\">" });
                                foreach (DataRow row3 in table3.Rows)
                                {
                                    str3 = str;
                                    str = str3 + "<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" />" + row3["value"].ToString();
                                }
                                str3 = str;
                                str = str3 + "<input name=\"hdAtrributes\" type=\"hidden\" value=\"cbAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>";
                                goto Label_0927;

                            case 5:
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" onfocus=\"new WdatePicker(this,'%Y-%M-%D',true,'default')\" style=\"ime-mode:disabled\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" });
                                goto Label_0927;
                        }
                    }
                Label_0927: ;
                }
                str = str + "</table>";
            }
            catch
            {
            }
            return str;
        }

        [AjaxMethod]
        public string GetBrandByCategoryIdentity(object categoryIdentity)
        {
            string str = "";
            try
            {
                Array array = categoryIdentity.ToString().Split(new char[] { ',' });
                DataTable table = new DataTable();
                ArrayList list = new ArrayList();
                bool flag = false;
                foreach (string str2 in array)
                {
                    DataTable brandByCategoryIdentity = this.theBrand.GetBrandByCategoryIdentity(Convert.ToInt32(str2));
                    if (!flag)
                    {
                        table = brandByCategoryIdentity.Clone();
                        table.Clear();
                        flag = true;
                    }
                    foreach (DataRow row in brandByCategoryIdentity.Rows)
                    {
                        if (!list.Contains(row["id"].ToString()))
                        {
                            list.Add(row["id"].ToString());
                            table.Rows.Add(row.ItemArray);
                        }
                    }
                }
                str = str + "<select id=\"selectBrand\" name=\"brand\"><option value=\"0\">请选择品牌...</option>";
                foreach (DataRow row in table.Rows)
                {
                    string str4 = str;
                    str = str4 + "<option value=\"" + row["id"].ToString() + "\">" + row["brandName"].ToString() + "</option>";
                }
                str = str + "</select>";
            }
            catch
            {
            }
            return str;
        }

        [AjaxMethod]
        public string GetSpecificationsById(object id)
        {
            string str = "";
            try
            {
                DataTable specificationsById = this.theSpecifications.GetSpecificationsById(Convert.ToInt32(id));
                string str2 = "";
                if (Convert.ToInt32(specificationsById.Rows[0]["showType"]) == 0)
                {
                    str2 = "平铺显示";
                }
                else
                {
                    str2 = "下拉显示";
                }
                string str4 = str;
                str = str4 + "<div style=\"font-weight:bold; height:20px;\">" + specificationsById.Rows[0]["specificationsName"].ToString() + "</div><div style=\"height:20px;\">" + specificationsById.Rows[0]["remark"].ToString() + "【显示类型：" + str2 + "】</div><div>";
                DataTable specificationsValueBySpecifications = this.theSpecificationsValue.GetSpecificationsValueBySpecifications(Convert.ToInt32(id));
                foreach (DataRow row in specificationsValueBySpecifications.Rows)
                {
                    if (Convert.ToInt32(specificationsById.Rows[0]["specificationsType"]) == 0)
                    {
                        str = str + "<span style=\"border:solid 1px #646464;\">" + row["specificationsValueName"].ToString() + "</span>&nbsp;&nbsp;";
                    }
                    else
                    {
                        str4 = str;
                        str = str4 + "<span style=\"border:solid 1px #646464;\"><img alt=\"" + row["specificationsValueName"].ToString() + "\" src=\"" + row["specificationsValueImage"].ToString() + "\" /></span>&nbsp;&nbsp;";
                    }
                }
                str = str + "</div>";
            }
            catch
            {
            }
            return str;
        }

        [AjaxMethod]
        public Array GetSpecifications()
        {
            ArrayList list = new ArrayList();
            try
            {
                int companyId = new BasePage().CompanyId;
                DataTable specificationsByCompanyId = this.theSpecifications.GetSpecificationsByCompanyId(companyId);
                foreach (DataRow row in specificationsByCompanyId.Rows)
                {
                    list.Add(row["id"]);
                }
            }
            catch
            {
            }
            return list.ToArray();
        }

        [AjaxMethod]
        public string GetSpecificationsNameById(object id)
        {
            return this.theSpecifications.GetSpecificationsById(Convert.ToInt32(id)).Rows[0]["specificationsName"].ToString();
        }

        [AjaxMethod]
        public string GetSpecificationsValueNameById(object id)
        {
            string str = "<select name=\"SpecificationsValue" + id.ToString() + "\">";
            try
            {
                DataTable specificationsValueBySpecifications = this.theSpecificationsValue.GetSpecificationsValueBySpecifications(Convert.ToInt32(id));
                foreach (DataRow row in specificationsValueBySpecifications.Rows)
                {
                    object obj2 = str;
                    str = string.Concat(new object[] { obj2, "<option value=\"", row["id"], "\">", row["specificationsValueName"], "</option>" });
                }
            }
            catch
            {
            }
            return (str + "</select>");
        }

    }
}